package com.jy.datapipeline.export.quartz;

import com.jy.datapipeline.common.util.BeanFactoryUtil;
import com.jy.datapipeline.export.entity.ExpTaskExecuteDetailLogModel;
import com.jy.datapipeline.export.entity.ExpTaskExecuteLogModel;
import com.jy.datapipeline.export.service.ExpTaskExecuteDetailLogService;
import com.jy.datapipeline.export.service.ExpTaskExecuteLogService;
import com.jy.datapipeline.export.service.ExpTaskService;
import java.util.List;


import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

@Slf4j
//@Component
public class JobDetailTaskStatusByErrorHandler extends QuartzJobBean {
//    @Autowired
//    private  ExpTaskService expTaskService;
//    @Autowired
//    private  ExpTaskExecuteLogService expTaskExecuteLogService;
//    @Autowired
//    private  ExpTaskExecuteDetailLogService expTaskExecuteDetailLogService;
//    public JobDetailTaskStatusByErrorHandler(ExpTaskService expTaskService,
//        ExpTaskExecuteLogService expTaskExecuteLogService,
//        ExpTaskExecuteDetailLogService expTaskExecuteDetailLogService) {
//        this.expTaskService = expTaskService;
//        this.expTaskExecuteLogService = expTaskExecuteLogService;
//        this.expTaskExecuteDetailLogService = expTaskExecuteDetailLogService;
//    }



    @Override
    protected void executeInternal(JobExecutionContext context)  {
        ExpTaskService expTaskService = BeanFactoryUtil.getBean(ExpTaskService.class);
        ExpTaskExecuteLogService expTaskExecuteLogService = BeanFactoryUtil.getBean(ExpTaskExecuteLogService.class);
        ExpTaskExecuteDetailLogService expTaskExecuteDetailLogService = BeanFactoryUtil.getBean(ExpTaskExecuteDetailLogService.class);
        try {
            List<ExpTaskExecuteLogModel> expTaskExecuteLogModelList= expTaskExecuteLogService.findByStatus(1);
            if(expTaskExecuteLogModelList.size()>0){
                expTaskExecuteLogModelList.forEach( executeLogModelList ->{
                    List<ExpTaskExecuteDetailLogModel> list=  expTaskExecuteDetailLogService.
                        findBytaskExecutelogLogIdAndExpStatusLessThan(executeLogModelList.getTaskExecuteLogId(),0);
                    if(list.size()>0){
                        expTaskExecuteLogService.updateExeuceStatus(executeLogModelList.getTaskExecuteLogId(),3,0,"","");
                        expTaskService.updateExpTaskStatus(3,executeLogModelList.getTaskId(),"");
                    }

                });
            }
        }catch (Exception e){
            log.error("JobDetailTaskStatusByErrorHandler error",e);
        }

    }
}
